package com.imooc.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class add extends HttpServlet{
	
	/**
	 * 	使用Servlet完成两个数的加法计算器：
	 * 
		1、创建一个html页面，定义一个表单，点击计算按钮，提交给Servlet进行处理
		2、将表单数据传递给Servlet，并将计算结果打印输出在页面上。
	 */

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		String num1 = request.getParameter("num1");
		String num2 = request.getParameter("num2");
		int n1 = Integer.parseInt(num1);
		int n2 = Integer.parseInt(num2);
		int sum = 0;
		sum = n1 + n2;
		String html = "<h1>计算机结果为 :"+ sum + "!</h1>";
		
		//设置输出页面格式为utf-8
		response.setContentType("text/html;charset=utf-8");
		//输出结果
		PrintWriter out = response.getWriter();
		out.println(html);
		
	}
	
}
